package com.iplant.wms.serviceImpl.dao.hr;

import com.iplant.base.po.OutResult;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.enums.ExceptionTypes;
import com.iplant.base.utils.annotation.dao.DBBaseDAO;
import com.iplant.base.utils.general.CalendarTool;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.base.utils.sql.BaseDAO;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.wms.service.basicenum.wms.WMSStockAlarmStatus;
import com.iplant.wms.service.po.hr.WMSStockAlarmHR;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@DBBaseDAO(WMSStockAlarmHR.class)
public class WMSStockAlarmHRDAO extends BaseDAO {

    private static WMSStockAlarmHRDAO Instance = null;
    private static Logger logger = LoggerFactory.getLogger(WMSStockAlarmHRDAO.class);

    private WMSStockAlarmHRDAO() {
        super("");
        this.Init();

    }

    public static WMSStockAlarmHRDAO getInstance() {
        if (Instance == null)
            Instance = new WMSStockAlarmHRDAO();
        return Instance;
    }

    public List<WMSStockAlarmHR> WMS_SelectStockAlarmHRList(BMSEmployee wLoginUser,
                                                            Calendar wStartDate, Calendar wEndDate, Pagination wPagination, OutResult<Integer> wErrorCode) {
        Map<String, Object> wParams = new HashMap<>();
        wParams.put("StartDate", wStartDate);
        wParams.put("EndDate", wEndDate);

        return this.SelectPOList(wLoginUser, wParams, wPagination, wErrorCode);
    }

    /**
     * 将今日之前的所有没关闭的全部关闭
     *
     * @param wLoginUser
     * @param wErrorCode
     */
    public void WMS_UpdateStockAlarmHRStatusCancel(BMSEmployee wLoginUser,
                                                   OutResult<Integer> wErrorCode) {
        wErrorCode.set(0);
        try {

            String wSQLText = StringUtils.Format("update {0} t set t.Status=:wStatus " +
                            " where  t.ID>0 and t.Status = :wStatusActive " +
                            " and t.AlarmDate< :wNowDate ",
                    this.getTableName());

            Map<String, Object> wParams = new HashMap<>();
            wParams.put("wStatus", WMSStockAlarmStatus.Cancel.getValue());
            wParams.put("wStatusActive", WMSStockAlarmStatus.Alarm.getValue());
            wParams.put("wNowDate", CalendarTool.getDate());
            this.Update(wSQLText, wParams);

            WMSStockAlarmItemHRDAO.getInstance().WMS_UpdateStockAlarmHRStatusCancel(wLoginUser, wErrorCode);

        } catch (Exception e) {
            wErrorCode.Result = ExceptionTypes.DBSQL.getValue();
            log.error("WMS_UpdateStockAlarmHRStatusCancel", e);
        }
    }

    /**
     * 单据状态维护
     *
     * @param wLoginUser
     * @param wIDList
     * @param wErrorCode
     */
    public void WMS_UpdateStockAlarmHRStatus(BMSEmployee wLoginUser, List<Integer> wIDList,
                                                   OutResult<Integer> wErrorCode) {
        wErrorCode.set(0);
        try {
            if (wIDList == null || wIDList.size() == 0)
                return;

            wIDList.removeIf(p -> p <= 0);

            if (wIDList.size() == 0)
                return;

            String wSQLText = StringUtils.Format("update {0} t set   t.Status=" +
                            " ( select  case  when ifnull(sum(t2.Status=:wStatusActive),0) >0 " +
                            " then :wStatusActive else :wStatusCancel end   from  {1} t2" +
                            " where t.ID=t2.StockAlarmID  ) where  t.ID in ({2}) ",
                    this.getTableName(), this.getTableName("wms_stock_alarm_item"), StringUtils.Join(",", wIDList));

            Map<String, Object> wParams = new HashMap<>();
            wParams.put("wStatusCancel", WMSStockAlarmStatus.Cancel.getValue());
            wParams.put("wStatusActive", WMSStockAlarmStatus.Alarm.getValue());
            this.Update(wSQLText, wParams);

            WMSStockAlarmItemHRDAO.getInstance().WMS_UpdateStockAlarmHRStatusCancel(wLoginUser, wErrorCode);

        } catch (Exception e) {
            wErrorCode.Result = ExceptionTypes.DBSQL.getValue();
            log.error("WMS_UpdateStockAlarmHRStatusCancel", e);
        }
    }


}
